package com.util;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;

public class JarPathUtil {
    public static String getJarPath(){
        String filePath  ="";
        URL url = Test.class.getProtectionDomain().getCodeSource().getLocation();
        try {
            filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码，支持中文
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (filePath.endsWith(".jar")) {
            // 可执行jar包运行的结果里包含".jar"
            // 获取jar包所在目录
            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        }
        File file = new File(filePath);
        filePath = file.getAbsolutePath();//得到windows下的正确路径
        System.out.println("jar包所在目录："+filePath);
        return filePath;
    }
}
